import Vue from 'vue'
import VeeValidate, { Validator } from 'vee-validate'
import zh from 'vee-validate/dist/locale/zh_CN'

Validator.addLocale(zh);

const config = {
  locale: 'zh_CN'
}

Vue.use(VeeValidate, config);
const dictionary = {
  zh_CN: {
    messages: {
      required: (field) => "请填写" + field
    },
    attributes: { }
  }
}
Validator.updateDictionary(dictionary);

Validator.extend('phone', {
  getMessage: (field, [args]) => `请输入正确的手机号码`,
  validate: (value, [args]) => {
    const reg = /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
    return reg.test(value)
  }
});

// 自定义验证：金额
Validator.extend("matchMoney",{
  getMessage: (field) => '请填写合理金额',
  validate: (value) => {
    var re = new RegExp(/^([\-\+]?)([\d]+|[\d]{1,3}(\,[\d]{3})*)(\.[\d]{1,2})?/g);
    return re.test(value);
  }
});
  // 自定义验证：金额最低值
Validator.extend("matchMoneyMin", {
  getMessage: (field, params) => '单笔最小金额'+ params,
  validate: (value, params) => {
    var re = false;
    value = value.match(/[\+|\-|\d|\.]/g).join("");
    if (parseInt(value, 10) >= parseInt(params, 10)) {
      re = true;
    }
    return re;
  }
});
// 自定义验证：金额最高值
Validator.extend("matchMoneyMax", {
  getMessage: (field, params) => '单笔最大金额' + params,
  validate: (value, params) => {
    var re = false;
    value = value.match(/[\+|\-|\d|\.]/g).join("");
    if (parseInt(value, 10) <= parseInt(params, 10)) {
      re = true;
    }
    return re;
  }
});
